Skip to content

perf(qwen4): optimize gated residual kernels and PDL - #1323

Merged
lightseek-bot merged 8 commits into
mainfrom
zt/qwen4_opt
Sep 1, 2026
Merged

perf(qwen4): optimize gated residual kernels and PDL#1323
lightseek-bot merged 8 commits into
mainfrom
zt/qwen4_opt

Conversation

@tuanzhangCS

@tuanzhangCS tuanzhangCS commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Move the Qwen4 gated-residual chain behind GPU-only tokenspeed-kernel APIs. Runtime code now owns parameters and checkpoint mapping only; grouped RMSNorm, mix, and combine implementations live in tokenspeed-kernel.
  • Fuse the down and inject projections, add a low-M persistent Triton path plus a portable Triton path, and keep the Blackwell CuTeDSL composition available for explicit tuning. Dispatch falls back to the deterministic general path when atomic accumulation is disabled.
  • Use stream-private persistent workspaces, preserve reduce-scatter row views, and apply non-power-of-two projection scaling after the projection to avoid checkpoint-weight quantization.
  • Prepare CuTeDSL's rank-padded derived weights once outside forward/CUDA Graph capture. Their shape and address stay fixed; supported online weight synchronization refreshes the same allocation through the weight loader, while forward performs a read-only lookup and rejects unprepared state. Validate grouped RMSNorm output buffers consistently for empty and non-empty inputs.
  • Remove redundant runtime CPU/CUDA branches, the registered CPU reference backend, and the public mix-epilogue wrapper. Numerical references remain test-only.

Performance

Compared with baseline 63f4585 on an NVIDIA B200 using BF16, PDL enabled, and CUDA Graph replay. The benchmark covers the full runtime grouped RMSNorm + gated-residual mix + combine chain; values are medians from five interleaved runs.

Tokens Baseline PR Improvement
1 18.570 us 16.399 us 11.69%
8 24.596 us 16.399 us 33.33%
16 24.576 us 16.398 us 33.28%
24 39.245 us 37.370 us 4.78%
32 40.491 us 38.291 us 5.43%
128 55.627 us 53.011 us 4.70%

The CUDA Graph geometric-mean improvement across the tested shapes is 16.57%, with no regression in any tested shape. The persistent Triton path provides the largest gain through 16 tokens; the general path remains 4.70%-5.43% faster above that range. CuTeDSL remains available for explicit tuning rather than default dispatch.

Test Plan

  • pre-commit run --all-files
  • Hyperconnection GPU numerics, dispatch, CUDA Graph, fixed-shape weight reload, PDL, and concurrent-stream tests: 37 passed
  • CuTeDSL low-latency BF16 router tests: 29 passed
  • Layernorm tests: 41 passed
  • Kernel registry and selection tests: 237 passed, 108 platform skips
  • Runtime kernel-boundary and weight-loader tests: 5 passed
  • ServerArgs attention-backend tests: 22 passed

The focused Qwen4 test module is not collectable in the current local environment because its installed tuning dependency does not yet accept the tensor_initializers argument; the changed paths are covered by the focused runtime and kernel suites above.

Signed-off-by: tuanzhangCS <tuan@lightseek.org>
@tuanzhangCS
tuanzhangCS requested review from a team as code owners August 31, 2026 12:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-01T04:16:50.420831Z d66622b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tuanzhangCS
tuanzhangCS marked this pull request as draft August 31, 2026 12:03
Signed-off-by: tuanzhangCS <tuan@lightseek.org>
@tuanzhangCS
tuanzhangCS marked this pull request as ready for review September 1, 2026 02:29

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 878bd8970c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tokenspeed-kernel/python/tokenspeed_kernel/ops/hyperconnection/cute_dsl.py Outdated
@tuanzhangCS
tuanzhangCS marked this pull request as draft September 1, 2026 02:39
Signed-off-by: tuanzhangCS <tuan@lightseek.org>
@tuanzhangCS
tuanzhangCS marked this pull request as ready for review September 1, 2026 03:10

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f39e97c459

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tokenspeed-kernel/python/tokenspeed_kernel/ops/layernorm/triton.py Outdated
Signed-off-by: tuanzhangCS <tuan@lightseek.org>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e7a7e50557

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tokenspeed-kernel/python/tokenspeed_kernel/ops/hyperconnection/cute_dsl.py Outdated
Signed-off-by: tuanzhangCS <tuan@lightseek.org>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 767b9c0e0a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +460 to +462
stream_id = int(torch.cuda.current_stream(device).cuda_stream)
key = (device_index, stream_id, projection_rows)
workspace = _PERSISTENT_WORKSPACES.get(key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Isolate persistent workspaces by captured graph

When multiple CUDA graphs are captured on the same stream and later replayed concurrently on different streams, this stream-only key makes every graph capture the same raw and counters pointers. The runtime graph wrapper captures all batch-size variants on one shared capture stream, so concurrent use of those graph executables would let persistent kernels zero and atomically update the same barrier state, potentially corrupting projections or hanging in _grid_barrier; give overlapping graph executions distinct workspace storage rather than keying solely by the capture stream handle.

Useful? React with 👍 / 👎.

Comment thread tokenspeed-kernel/python/tokenspeed_kernel/ops/hyperconnection/README.md Outdated
borontion
borontion previously approved these changes Sep 1, 2026
Signed-off-by: tuanzhangCS <tuan@lightseek.org>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d66622b074

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

raise ValueError(
f"{name} must have last dimension {width}, got {tuple(value.shape)}"
)
return value.reshape(-1, width)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve last-dimension strides before launching epilogues

When a caller passes a 2-D feature-strided view such as base[:, ::2] with the required logical width, this reshape can preserve its non-unit last-dimension stride. Both _mix_epilogue_kernel and _combine_kernel then address features as ptr + column without receiving a column stride, so the general mix and combine paths read incorrect elements; the previous runtime implementations explicitly made these operands contiguous. Make the flattened operands contiguous when their last-dimension stride is not one, or pass that stride to the kernels.

Useful? React with 👍 / 👎.

@lightseek-bot
lightseek-bot merged commit a895243 into main Sep 1, 2026
15 of 24 checks passed
@lightseek-bot
lightseek-bot deleted the zt/qwen4_opt branch September 1, 2026 04:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants